package com.ea.common.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 57861
 * @Description 时间工具类
 * @className DateTimeUtils
 * @date 2022-04-18 20:38:00
 */
public class DateTimeUtils {

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneOffset.systemDefault()).toInstant());
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    public static LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 获取指定时间的当天的时间起点
     * @param date
     * @return
     */
    public static LocalDate getDayBeginning(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate;
    }


    /**
     * 获取指定时间的当天的结束时间，精确到毫秒
     * @param date
     * @return
     */
    public static LocalDateTime getDayEnding(Date date) {
        return getDayBeginning(date).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime().plusDays(1L).plus(-1L, ChronoUnit.MILLIS);
    }

    /**
     * 获取指定时间的当月的时间起点
     * @param date
     * @return
     */
    public static LocalDate getMonthBeginning(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //Calendar的月份是从0开始的
        LocalDate localDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);
        return localDate;
    }

    /**
     * 获取指定时间的当月的结束时间，精确到毫秒
     * @param date
     * @return
     */
    public static LocalDateTime getMonthEnding(Date date) {
        return getMonthBeginning(date).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime().plusMonths(1).plus(-1, ChronoUnit.MILLIS);
    }

    /**
     * 获取指定时间的当年时间起点
     * @param date
     * @return
     */
    public static LocalDate getYearBeginning(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        LocalDate localDate = LocalDate.of(calendar.get(Calendar.YEAR), 1, 1);
        return localDate;
    }

    /**
     * 获取指定时间的当年的结束时间，精确到毫秒
     * @param date
     * @return
     */
    public static LocalDateTime getYearEnding(Date date) {
        return getYearBeginning(date).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime().plusYears(1).plus(-1, ChronoUnit.MILLIS);
    }

    /**
     * 获取指定时间的当周的时间起点
     * @param date
     * @return
     */
    public static LocalDate getWeekBeginning(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        DayOfWeek dayOfWeek=localDate.getDayOfWeek();
        LocalDate weekBeginning=null;
        //calendar将星期天作为第一天，改成符合中国人习惯的，周一作为第一天
        switch (dayOfWeek) {
            case MONDAY:
                weekBeginning=localDate;
                break;
            case TUESDAY:
                weekBeginning=localDate.plusDays(-1);
                break;
            case WEDNESDAY:
                weekBeginning=localDate.plusDays(-2);
                break;
            case THURSDAY:
                weekBeginning=localDate.plusDays(-3);
                break;
            case FRIDAY:
                weekBeginning=localDate.plusDays(-4);
                break;
            case SATURDAY:
                weekBeginning=localDate.plusDays(-5);
                break;
            case SUNDAY:
                weekBeginning=localDate.plusDays(-6);
                break;
        }
        return weekBeginning;
    }

    /**
     * 获取指定时间的当周的结束时间，精确到毫秒
     * @param date
     * @return
     */
    public static LocalDateTime getWeekEnding(Date date) {
        return getWeekBeginning(date).atStartOfDay(ZoneId.systemDefault()).toLocalDateTime().plusWeeks(1).plus(-1,ChronoUnit.MILLIS);
    }

    /**
     * 获取yyyy-MM-dd格式字符串日期
     *
     * @return
     */
    public static String getCurrentDateYYYY_MM_DDStr() {
        return new SimpleDateFormat(DATE_FORMAT).format(new Date());
    }

    /**
     * 获取当前的时间yyyyMMddHHmmsss的String
     *
     * @return String
     */
    public static String getNowStringMillisecond() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmsss");
        return format.format(new Date());
    }

    /**
     * 获取当前的时间yyyyMMddHH的String
     *
     * @return String
     */
    public static String getNowStringHour() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
        return format.format(new Date());
    }

    /**
     * 获取当前的时间yyyyMMddHH的String
     *
     * @return String
     */
    public static String getNowStringDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }

    /**
     * 获取昨天时间yyyy-MM-dd格式
     *
     * @return String
     */
    public static String getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat(DATE_FORMAT).format(cal.getTime());
    }

}
